﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using System.Drawing;
namespace Deloitte.MSWord
{
    public class WordHelper
    {
        public static void InsertImage(Word.Document document, string imagePath)
        {
            Bitmap image = new Bitmap(imagePath);
            WordHelper.InsertImage(document, image);
        }
        
        public static void InsertImage(Word.Document document, Bitmap image)
        {
            object missing = Missing.Value;
            object pos1 = document.StoryRanges[Microsoft.Office.Interop.Word.WdStoryType.wdMainTextStory].End - 1;
            WordHelper.InsertImage(document.Range(ref pos1, ref missing), image);
            object pos2 = document.StoryRanges[Microsoft.Office.Interop.Word.WdStoryType.wdMainTextStory].End;
            Word.Range range = document.Range(ref pos1, ref pos2);
            range.InsertParagraphAfter();
            range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
        }
        public static void InsertImage(Word.Range range, Bitmap image)
        {
            object missing = Missing.Value;
            System.Drawing.Bitmap img = image;
            string flName = System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache).ToString() + @"\" + "tmp.bmp";
            img.Save(flName);
            range.InlineShapes.AddPicture(flName, ref missing, ref missing, ref missing);
        }
    }
}
